<?php
// +----------------------------------------------------------------------
// | CoreThink [ Simple Efficient Excellent ]
// +----------------------------------------------------------------------
// | Copyright (c) 2014 http://www.corethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: jry <598821125@qq.com> <http://www.corethink.cn>
// +----------------------------------------------------------------------
namespace Admin\Controller;
use Think\Controller;


class FriendController extends AdminController
{
	public function index()
	{
		$list=D("Friendlinks")->page(!empty($_GET['p'])?$_GET['p']:1,C("ADMIN_PAGE_ROWS"))->order("id desc")->select();

		$page=new \Common\Util\Page(D("Friendlinks")->count(),C("ADMIN_PAGE_ROWS"));
		
		$builder = new \Common\Builder\ListBuilder();
		$builder->setMetaTitle("友情链接列表") //设置页面标题
		->addTopButton('addnew') //添加新增按钮
		//->setSearch('请输入品牌ID/名称', U('index'), $list)
		->addTableColumn('id', 'ID')
		->addTableColumn('friendlinkName', '网站名称')
		->addTableColumn('friendlinkUrl', '网址')
		->addTableColumn('friendlinkIco', '图标', 'image')
		->addTableColumn('right_button', '操作', 'btn')
		->setTableDataList($list) //数据列表
		->setTableDataPage($page->show())  //数据列表分页
		->addRightButton('edit')    //添加编辑按钮
		
		->addRightButton('delete',array('href'=>U('Admin/Friend/delete',array('id'=>'__data_id__'))))  //添加禁用/启用按钮
		->display();
	}
	public function add()
	{
		if(IS_POST)
		{
			$friend_object = D('Friendlinks');
			$data = $friend_object->create($_POST);
			if($data){
				$id = $friend_object->add();
				 
				if($id){
					$this->success('新增成功', U('index'));
				}else{
					$this->error('新增失败');
				}
			}else{
				$this->error($friend_object->getError());
			}
			
		}else{
			$builder = new \Common\Builder\FormBuilder();
			$builder->setMetaTitle('新增链接')  //设置页面标题
			->setPostUrl(U('add')) //设置表单提交地址
			->addFormItem('friendlinkName', 'text', '网站名称', '')
			->addFormItem('friendlinkIco', 'picture', '网站图标', '')
			->addFormItem('friendlinkUrl', 'text', '网站地址', '请加上http://')
			->addFormItem('friendlinkSort', 'text', '网站排序号','')
			->display();
		}
	}
	public function edit()
	{
		$id=I("id");
		if(IS_POST)
		{
			
			$friend_object = D('Friendlinks');
			$data = $friend_object->create($_POST);
			if($data){
				$id = $friend_object->save();
					
				if($id){
					$this->success('修改成功', U('index'));
				}else{
					$this->error('修改失败');
				}
			}else{
				$this->error($friend_object->getError());
			}
			
		}else{
			if($id)
			{
					$list=D("Friendlinks")->find($id); 
					$builder = new \Common\Builder\FormBuilder();
					$builder->setMetaTitle('编辑')  //设置页面标题
					->setPostUrl(U('edit')) //设置表单提交地址
					->addFormItem('id','hidden', 'ID', '',$id)
					->addFormItem('friendlinkName','text', '网站名称', '')
					->addFormItem('friendlinkIco','picture', '网站图标', '')
					->addFormItem('friendlinkUrl','text', '网站地址', '')
					->addFormItem('friendlinkSort','text', '网站排序号','')
					->setFormData($list)
					->display();
			}
		}
	}
	function delete($id)
	{
		if($id)
		{
			$res=D("Friendlinks")->delete($id);
			if($res)
			{
				$this->success('删除成功',U("index"));
			}else{
				$this->error("删除失败");
			}
		}
	}
}